<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/3
 * Time: 13:55
 */

namespace Admin\Model;
use Think\Model;

class ItemModel extends Model
{
    protected $tableName = "item";
    protected $tablePrefix = "dangjian_";
    protected $showRow = 10;
    //查询所有项目
    public function queryAllItem ($data)
    {
        $item = $data['item'];
        $status = $data['status'];
        $map = array();
        if(empty($item)){
        }else{
            $map['item'] = array("LIKE", '%' . $item . '%');
        }
        if(empty($status)){
        }else{
            if($status == 1){
                $map['end_time'] = array("gt",date("Y-m-d H:i:s"));
            }else{
                $map['end_time'] = array("elt",date("Y-m-d H:i:s"));
            }
        }
        $count = $this->where($map)->count();
        $page = new \Think\Page($count,$this->showRow);
        $show = $page->show();
        $list = $this->where($map)
            ->order("create_time desc")
            ->limit($page->firstRow.','.$page->listRows)
            ->select();
        $res['show'] = $show;
        $res['list'] = $list;
        return $res;
    }

    //保存新项目
    public function saveItem($data)
    {

        echo $res = $this->data($data)->add();
        if($res > 0){
            return 1;
        }
        return 0;
    }

    //查看捐款项目详情根据项目id
    public function queryItemDetailById($id)
    {
        $info = $this->where("id=$id")->field("item,introduce")->find();
        return $info;
    }

    public function queryItem ()
    {
        $res = $this -> field("id,item,end_time")->select();
        return $res ;
    }

    public function queryAllItems ()
    {
        $res = $this -> field("id,item")->select();
        return $res ;
    }
}